<?php
class Admin extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper("authentication_helper");
		$this->load->library('session');
	}

	function index(){
		if(!$this->session->userdata('is_logged')){
			$data['page_title'] = "Admin Authentication - Alt Vid Pro.";
			$this->load->view("admin/admin_authentication",$data);
		}else {
			redirect('admin/home', 'refresh');
			/*if (!$this->session->userdata('redirect_url')) {
				redirect($this->session->userdata('redirect_url'), 'refresh');
				$this->session->unset_userdata('redirect_url');
			} else {
				redirect('admin_home', 'refresh');
			}*/
			
		}
	}
	
	function authenticate(){
		$username = $this->input->post("username"); 
		$password = $this->input->post("password");
		$key = md5('altvidpro');
		$passtemp = generatePassword($password, $key);
		if ($passtemp === '46d8a1dabdfc9fd55f173b8b2c612a4c' && $username === 'admin') {
			$sess_array = array(
				'username' => 'admin',
			);
			$this->session->set_userdata('is_logged', $sess_array);
			$redirect = $this->session->userdata('redirect_url');
			if ($redirect === false){
				$redirect = 'admin';
			}
			echo json_encode(array('success'=>true,"url"=>$redirect));
		}else{
			echo json_encode(array('success'=>false,"reason"=>$passtemp));
		}
	}
	
	function logout(){
		$this->session->unset_userdata('is_logged');
		echo json_encode(array('success'=>true));
	}
}
